package com.lbs.jiepiaisi.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.lbs.jiepiaisi.R;

import com.lbs.jiepiaisi.activity.AlarmListActivity;
import com.lbs.jiepiaisi.adapter.AlarmListAdapter;
import com.lbs.jiepiaisi.base.BaseFragment;
import com.lbs.jiepiaisi.base.ErrorException;
import com.lbs.jiepiaisi.base.ProgressObserver;
import com.lbs.jiepiaisi.bean.AlarmBean;
import com.lbs.jiepiaisi.config.Api;
import com.lbs.jiepiaisi.manager.App;
import com.lbs.jiepiaisi.manager.SharePreferenceManager;
import com.lbs.jiepiaisi.utils.ButtonUtil;
import com.lbs.jiepiaisi.utils.Constant;
import com.lbs.jiepiaisi.utils.Constant1;
import com.lbs.jiepiaisi.utils.RxSchedulers;
import com.lbs.jiepiaisi.utils.SpUtil;
import com.lbs.jiepiaisi.view.CustomLoadMoreView;

import java.util.ArrayList;
import java.util.List;


public class AlarmListFragment extends BaseFragment {

    private RecyclerView recyclerView;
    private SwipeRefreshLayout refreshLayout;

    private AlarmListAdapter mAdapter;
    private int page = 1;//页数
    private int size = 10;//记录数
    private List<AlarmBean.Result.Item> listItem = new ArrayList();
    private String deptId;

    @Override
    public View initView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment_alarm_list,null);
        return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //SharePreferenceManager mSharePreference = SharePreferenceManager.getInstance(App.instance.getApplicationContext());
        //deptId = mSharePreference.getInt(Constant.ACCOUNT_DEPTID,Constant.SP_NULL) + "";//部门编码
        deptId = (String)SpUtil.get(Constant1.DEPTID,"");
        Log.e("AlarmListFragment","dept = "+deptId);
    }

    @Override
    protected void getExtras(View view) {}

    @Override
    protected void findView(View view) {
        recyclerView = view.findViewById(R.id.recyclerView);

        refreshLayout = view.findViewById(R.id.refreshLayout);
        refreshLayout.setColorSchemeResources(R.color.blue);

        mAdapter = new AlarmListAdapter(R.layout.list_item_platform_warning, listItem);
        mAdapter.bindToRecyclerView(recyclerView);
        mAdapter.disableLoadMoreIfNotFullPage();
        mAdapter.setLoadMoreView(new CustomLoadMoreView());

        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(mAdapter);
        mAdapter.setEnableLoadMore(true);

        refresh();
    }

    @Override
    protected void setListener(View view) {
        refreshLayout.setOnRefreshListener(() -> {
            mAdapter.setEnableLoadMore(false);
            page = 1;
            refresh();
        });

        mAdapter.setOnLoadMoreListener(()->{
            refresh();
        },recyclerView);

        mAdapter.setOnItemChildClickListener((adapter, view1, position) -> {
            AlarmBean.Result.Item item = listItem.get(position);
            if(ButtonUtil.isFastClick()){
                Intent intent = new Intent();
                intent.putExtra("eqNo", item.getEqNo());
                intent.putExtra("deptId",deptId);
                intent.putExtra("ownerName",item.getOwnerName());
                intent.setClass(mContext, AlarmListActivity.class);
                mContext.startActivity(intent);
            }
        });
    }

    private void showEmpty() {
        mAdapter.setEmptyView(R.layout.layout_empty);
    }

    public void refresh() {
        Api.getInstance().createService().loadAlarmList(deptId,size,page,"")
                .compose(RxSchedulers.observableIO2Main(mContext))
                .subscribe(new ProgressObserver<AlarmBean>(mContext,false) {
                    @Override
                    public void onSuccess(AlarmBean result) {
                        refreshLayout.setRefreshing(false);
                        List data = result.getResult().getItems();
                        if (page == 1) {
                            if (data.size() == 0) showEmpty();
                            listItem.clear();
                            listItem.addAll(data);
                            mAdapter.setNewData(listItem);
                        } else {
                            mAdapter.addData(data);
                        }

                        if (data.size() < size) {
                            mAdapter.loadMoreEnd();
                        } else {
                            mAdapter.loadMoreComplete();
                        }
                        page++;
                    }

                    @Override
                    public void onFailure(ErrorException e) {
                        refreshLayout.setRefreshing(false);
                        mAdapter.loadMoreFail();
                        showEmpty();
                        Log.e("eeeee","告警错误 = "+e.getCode());
                    }
                });
    }

}
